using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrOccupation : System.Web.UI.UserControl
{
    protected string strOccupation = "-1";
    protected string strWidth = "230";
    protected string strValidatinoGroup = "";
    protected string strErrorMessage = "Choose occupation.";
    protected bool bEnabled = true;
    bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dtOccupation = General.GetComboItem(Resources.CarInsurance.OccupationList, Resources.CarInsurance.OccupationSortBy,true);
            ddlOccupation.DataSource = dtOccupation;
            ddlOccupation.DataTextField = dtOccupation.Columns[0].ColumnName.ToString();
            ddlOccupation.DataValueField = dtOccupation.Columns[1].ColumnName.ToString();
            ddlOccupation.DataBind();
            ddlOccupation.Items.Insert(0, new ListItem(Resources .CarInsurance .Select , "-1"));

            ddlOccupation.Attributes.Add("onblur", "ShowValidationEffect('" + ddlOccupation.ClientID + "','" + rfvOccupation.ClientID + "');");
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvOccupation.Enabled && rfvOccupation.ValidationGroup == strValidationGroup)
        {
            rfvOccupation.Validate();
            if (rfvOccupation.IsValid == false)
            {
                ddlOccupation.ControlStyle.CssClass = "proposal-combo validationError";
            }
            else
            {
                ddlOccupation.ControlStyle.CssClass = "proposal-combo";
            }
        }
        else
        {
            ddlOccupation.ControlStyle.CssClass = "proposal-combo";
        }
    }

    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the value is -1
    //        if (args.Value != "-1")
    //        {
    //            args.IsValid = true;
    //        }
    //        else
    //        {
    //            args.IsValid = false;
    //        }
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvOccupation.Enabled = bValue;
    }

    public string Occupation
    {
        get { return strOccupation; }
        set { strOccupation = value; }
    }

    public string Width
    {
        get { return strWidth; }
        set { strWidth = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlOccupation.SelectedIndex = 0;
        ddlOccupation.Enabled = bEnabled;
        rfvOccupation.Enabled = bEnabled;
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        if (strOccupation != string.Empty)
        {
            if (strOccupation != "-1")
            {

                ddlOccupation.Text = strOccupation;
            }
        }

        if (strWidth != string.Empty)
        {
            int _intWidth = 0;
            if (int.TryParse(strWidth, out _intWidth))
            {
                ddlOccupation.Width = Unit.Pixel(Convert.ToInt32(strWidth));
            }
            else
            {
                ddlOccupation.Width = Unit.Pixel(230);
            }
        }

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
                rfvOccupation.ValidationGroup = strValidatinoGroup;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }


        if (strErrorMessage != string.Empty)
        {
            //rfvOccupation.ErrorMessage = strErrorMessage;
        }
    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlOccupation.Text = strOccupation;

            string orgOccupationText = ddlOccupation.SelectedItem.Text;
            string orgOccupationValue = ddlOccupation.SelectedItem.Value;

            ddlOccupation.Items.Clear();

            ddlOccupation.Items.Add(new ListItem(orgOccupationText, orgOccupationValue));
        }
    }

    public string GetOccupation()
    {
        strOccupation = ddlOccupation.Text;
        return strOccupation;
    }
}
